﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Twodi.Interfaces;
using System.ComponentModel;
using Twodi.Structs;

namespace Twodi.Aspect.Implemented
{
    public class AnimationAspect : AbstractAspect, IAnimation
    {
        protected int animationIndex = -1;

        protected BindingList<Animation> animations;

        public AnimationAspect()
        {
            animations = new BindingList<Animation>();

            Animations.ListChanged += (sender, e) =>
            {
                onAnimationListChanged(EventArgs.Empty);
            };
        }

        public void Add(params Animation[] animations)
        {
            for (int i = 0; i < animations.Length; i++)
            {
                this.animations.Add(animations[i]);
            }
        }

        public Animation Search(string animationName)
        {
            for (int i = 0; i < this.animations.Count; i++)
            {
                if (Animations[i].Name == animationName)
                {
                    return Animations[i];
                }
            }

            return null;
        }

        public void Remove(string animationName)
        {
            for (int i = 0; i < this.animations.Count; i++)
            {
                if (Animations[i].Name == animationName)
                {
                    Animations.Remove(Animations[i]);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public int AnimationIndex
        {
            get { return this.animationIndex; }
            set
            {
                this.animationIndex = value;
                onAnimationIndexChanged(EventArgs.Empty);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public BindingList<Animation> Animations
        {
            get
            {
                return this.animations;
            }
        }

        public Animation Animation
        {
            get
            {
                if (Animations.Count == 0 || AnimationIndex < 0) return null;
                return Animations[AnimationIndex];
            }
        }

        public string Name
        {
            get { return this.Animations[AnimationIndex].Name; }
            set
            {
                AnimationIndex = -1;
                for (int i = 0; i < Animations.Count; i++)
                {
                    if (Animations[i].Name.ToUpper() == value.ToUpper())
                    {
                        AnimationIndex = i;
                        onAnimationChanged(EventArgs.Empty);
                        break;
                    }
                }

                if (AnimationIndex == -1)
                {
                    throw new Exception("Animation not found.");
                }
                else
                {
                    Animation.Reset();
                };
            }
        }

        public event EventHandler AnimationIndexChanged;

        public event EventHandler AnimationChanged;

        public event EventHandler AnimationListChanged;

        protected virtual void onAnimationIndexChanged(EventArgs e)
        {
            if (AnimationIndexChanged != null)
            {
                AnimationIndexChanged(this, e);
            }
        }

        protected virtual void onAnimationChanged(EventArgs e)
        {
            if (AnimationChanged != null)
            {
                AnimationChanged(this, e);
            }
        }

        protected virtual void onAnimationListChanged(EventArgs e)
        {
            if (AnimationListChanged != null)
            {
                AnimationListChanged(this, e);
            }
        }
    }
}
